# -*- coding: utf-8 -*-
import json
import re

import scrapy


class Item(dict):
    class_name = ''

    def __init__(self, selector, class_name=''):
        super().__init__()
        self._selector = selector
        self.class_name = class_name

    def add_path(self, attr, xpath, begin=0, end=None):
        if end is None:
            self[attr] = self._selector.xpath(xpath).get(begin)
            return
        self[attr] = self._selector.xpath(xpath).getall()[begin:end + 1]


class CarSubSpider(scrapy.Spider):
    name = 'car_sub'
    # allowed_domains = ['auto.16888.com/']
    start_urls = ['http://db.auto.sohu.com/brand_191/home/iframe.html']

    # 访问每个品牌的主页
    def parse(self, response):
        i = 191
        n = 279
        while i <= n:
            url = 'http://db.auto.sohu.com/brand_' + str(i) + '/home/iframe.html'
            i += 1
            yield scrapy.Request(url, callback=self.solve_detail)
        # for e in response.xpath('//li[@class="close_child"]'):
        #     brand=e.xpath('h4[@class="brand_tit"]//a/text()').extract()[0].split(',')[1].replace('\'','').strip()

    # 访问每个车系的主页，清除掉未上市或者停售的车系
    def solve_detail(self, response):
        # print(response.url)
        # res=response.xpath('//div[@class="tit brandtit"]').extract()
        # print(res)
        # name_prefix=response.xpath('//div[@class="tit brandtit"]/h2/text()').extract()[0]
        # print('-------------------------------------',name_prefix)
        for e in response.xpath('//div[@class="tabcon cur"]/ul/li/a'):
            # 排除停售车辆，还未上市的车辆
            tip = e.xpath('span/span/@class').extract()
            if (len(tip) > 0 and (tip[0] == 'tip_stop' or tip[0] == 'tip_start')):
                continue
            # name_suffix=e.xpath('span/text()').extract()[0].strip()
            # # 车系名字
            # name=name_prefix+name_suffix

            # 车系主页
            url = 'http:' + e.xpath('@href').extract()[0] + '/trim.html'
            # print(e.xpath('@href').extract())
            # print(url)
            yield scrapy.Request(url, callback=self.solve_name)

    # 处理每个车系主页，提取出每个车款的主页
    def solve_name(self, response):
        for e in response.xpath('//div[@class="on_sell"]//li'):
            url = e.xpath('a/@href').extract()
            if (len(url) > 0):
                yield scrapy.Request('http:' + url[0] + 'trim.html', callback=self.solve_subName)

    # 处理每个车款主页，提取车款的重要数据 trimParam，过滤掉反正则的标签属性，存入json
    def solve_subName(self, response):
        print('resolve_name')
        res = response.xpath('//script[@type="text/javascript" and not(@src)]')
        js_selector = res[len(res) - 1].xpath('text()')
        str = js_selector.extract()[0]
        str = str.replace('\n', '').replace('\t', '').replace(' ', '')
        s = re.search('trimParam=(.*);$''', str).groups()
        if (len(s) == 0):
            return
        s = s[0]
        s2 = re.sub('<(.*)>', '', s)
        res = json.loads(s2)
        if (len(res) == 0):
            return
        res = res[0]
        conf = res['SIP_T_CONF']
        item = Item('subcar')
        item['subName'] = res["SIP_T_NAME"]
        item['Name'] = res["modelName"]
        item['speed'] = int(conf["SIP_C_112"].get("v", 0))
        item['price'] = res["SIP_T_PRICE"]
        item['date'] = conf["SIP_C_109"].get("v", "-")
        item['seat'] = int(conf["SIP_C_126"].get("v", 0).split('-')[0])
        item['emissionStandrad'] = conf["SIP_C_155"].get("v", "-")
        item['powerType'] = conf["SIP_C_303"].get("v", "-")
        item['output'] = float(conf["SIP_C_136"].get("v", 0))
        item['type'] = conf["SIP_C_106"].get("v", "-")
        if '座' in item['type']:
            str=item['type']
            item['type']=str[str.find('座')+1:]
        item['brand'] = res["brandName"]+res["modelName"]
        x = conf["SIP_C_294"].get("v", "--/--/--").split('/')[2]
        if x=='--':
            item['oil']=404
        else:
            item['oil']=float(x)
        item['accelerationTime'] = float(conf["SIP_C_113"].get("v", 0))
        item['auto'] = conf["SIP_C_158"].get("v", "手动")
        item['speedSection']=(int(item['speed'])+9)//10
        item['priceSection']=(int(item['price'])+9)//10
        item['grade']=0
        print(f'车款名subName=>{res["SIP_T_NAME"]}')
        print(f'车系名Name=>{res["modelName"]}')
        print(f'时速speed=>{conf["SIP_C_112"].get("v", 0)}')
        print(f'厂商指导价price=>{res["SIP_T_PRICE"]}')
        # 详细信息
        print(f'上市时间date=>{conf["SIP_C_109"].get("v", "-")}')
        print(f'座位数seat=>{conf["SIP_C_126"].get("v", 0)}')
        print(f'排放标准emissionStandrad=>{conf["SIP_C_155"].get("v", "-")}')
        print(f'动力类型powerType=>{conf["SIP_C_303"].get("v", "-")}')
        print(f'排量output=>{conf["SIP_C_136"].get("v", 0)}')
        print(f'车型type=>{conf["SIP_C_106"].get("v", "-")}')
        print(f'品牌名brand=>{res["brandName"]+res["modelName"]}')
        print(f'百公里综合油耗oil=>{conf["SIP_C_294"].get("v", "-/-/-")}')
        print(f'百公里加速所需时间accelerationTime=>{conf["SIP_C_113"].get("v", 0)}')
        print(f'变速箱auto=>{conf["SIP_C_158"].get("v", "手动")}')
        print(item)
        yield item

    pass
